home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / OutOfPhase1.1 Source / OutOfPhase Folder / Level 0 Macintosh 01Jan95 / StartupOpen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-27  |  9.4 KB  |  294 lines  |  [TEXT/KAHL]

  1. /* StartupOpen.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    System Dependency Library for Building Portable Software               */
  5. /*    Macintosh Version                                                      */
  6. /*    Written by Thomas R. Lawrence, 1993 - 1994.                            */
  7. /*                                                                           */
  8. /*    This file is Public Domain; it may be used for any purpose whatsoever  */
  9. /*    without restriction.                                                   */
  10. /*                                                                           */
  11. /*    This package is distributed in the hope that it will be useful,        */
  12. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  13. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                   */
  14. /*                                                                           */
  15. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  16. /*                                                                           */
  17. /*****************************************************************************/
  18.  
  19. #include "MiscInfo.h"
  20. #include "Debug.h"
  21. #include "Audit.h"
  22. #include "Definitions.h"
  23.  
  24. #ifdef THINK_C
  25.     #pragma options(pack_enums)
  26. #endif
  27. #include <memory.h>
  28. #include <files.h>
  29. #include <AppleEvents.h>
  30. #ifdef THINK_C
  31.     #pragma options(!pack_enums)
  32. #endif
  33.  
  34. #include "StartupOpen.h"
  35. #include "Memory.h"
  36. #include "Array.h"
  37. #include "Files.h"
  38.  
  39.  
  40. /* this contains a list of things that have been received for opening.*/
  41. /* if StartupList is NIL, then the open event hasn't been received.  Otherwise, */
  42. /* it contains FileSpecs to be opened */
  43. static ArrayRec*                    StartupList = NIL;
  44.  
  45. /* debugging flag */
  46. EXECUTE(static MyBoolean    Initialized = False;)
  47.  
  48. /* flag that gets set True when a quit signal is received */
  49. static MyBoolean                    QuitPending = False;
  50.  
  51.  
  52. /* function to check to see that all required parameters have been gotten */
  53. static OSErr        MyGotRequiredParams(AppleEvent* theAppleEvent)
  54.     {
  55.         DescType        ReturnedType;
  56.         Size                ActualSize;
  57.         OSErr                Error;
  58.  
  59.         Error = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
  60.             &ReturnedType,NIL,0,&ActualSize);
  61.         if (Error == errAEDescNotFound)
  62.             {
  63.                 return noErr;  /* we got all the params, since no more were found */
  64.             }
  65.          else
  66.             {
  67.                 if (Error == noErr)
  68.                     {
  69.                         return errAEEventNotHandled;  /* missed some, so it failed */
  70.                     }
  71.                  else
  72.                     {
  73.                         return Error;  /* AEGetAttributePtr failed, so we return why */
  74.                     }
  75.             }
  76.     }
  77.  
  78.  
  79. /* handler for open application--presents an untitled document */
  80. static pascal    OSErr        MyHandleOApp(AppleEvent* theAppleEvent,
  81.                                                 AppleEvent* reply, long handlerRefcon)
  82.     {
  83.         OSErr        Error;
  84.  
  85.         APRINT(("+MyHandleOApp"));
  86.         Error = MyGotRequiredParams(theAppleEvent);
  87.         ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleOApp error."));
  88.         if (Error != noErr)
  89.             {
  90.                 APRINT(("-MyHandleOApp Error %s",Error));
  91.                 return Error;
  92.             }
  93.         StartupList = NewArray();
  94.         APRINT(("-MyHandleOApp"));
  95.         return noErr;
  96.     }
  97.  
  98.  
  99. /* handler for open documents */
  100. static pascal    OSErr        MyHandleODoc(AppleEvent* theAppleEvent,
  101.                                                 AppleEvent* reply, long handlerRefcon)
  102.     {
  103.         OSErr                        Error;
  104.         long                        Index,ItemsInList;
  105.         AEDescList            DocList;
  106.         Size                        ActualSize;
  107.         AEKeyword                Keywd;
  108.         DescType                ReturnedType;
  109.  
  110.         APRINT(("+MyHandleODoc"));
  111.         /* get the direct parameter--a descriptor list--and put it into DocList */
  112.         Error = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &DocList);
  113.         ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleODoc error."));
  114.         if (Error != noErr) return Error;
  115.         /* check for missing required parameters */
  116.         Error = MyGotRequiredParams(theAppleEvent);
  117.         if (Error != noErr) return Error;
  118.         ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleODoc error."));
  119.         /* count the number of descriptor records in the list */
  120.         Error = AECountItems(&DocList,&ItemsInList);
  121.         ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleODoc error."));
  122.         /* now get each descriptor record from the list, coerce the returned data */
  123.         /* to an FSSpec record, and open the associated file */
  124.         if (StartupList == NIL)
  125.             {
  126.                 StartupList = NewArray();
  127.             }
  128.         if (StartupList == NIL)
  129.             {
  130.                 /* error -- not enough memory to create the list */
  131.                 goto OutOfMemPoint;
  132.             }
  133.         for (Index=1; Index <= ItemsInList; Index += 1)
  134.             {
  135.                 FSSpec                    MyFSS;
  136.  
  137.                 Error = AEGetNthPtr(&DocList,Index,typeFSS,&Keywd,&ReturnedType,
  138.                     (void*)&MyFSS,sizeof(FSSpec),&ActualSize);
  139.                 ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleODoc error."));
  140.                 if (Error == noErr)
  141.                     {
  142.                         FSSpec*                    ArrayElement;
  143.                         long                        CharIndex;
  144.  
  145.                         ArrayElement = (FSSpec*)AllocPtrCanFail(sizeof(FSSpec),"StartupOpenFSSpec");
  146.                         if (ArrayElement == NIL)
  147.                             {
  148.                              FailurePoint1:
  149.                                 goto OutOfMemPoint;
  150.                             }
  151.                         *ArrayElement = MyFSS;
  152.                         if (!ArrayAppendElement(StartupList,ArrayElement))
  153.                             {
  154.                              FailurePoint2:
  155.                                 ReleasePtr((char*)ArrayElement);
  156.                                 goto FailurePoint1;
  157.                             }
  158.                         if (!Eep_RegisterFileSpec((FileSpec*)ArrayElement))
  159.                             {
  160.                                 ArrayDeleteElement(StartupList,
  161.                                     ArrayFindElement(StartupList,ArrayElement));
  162.                                 goto FailurePoint2;
  163.                             }
  164.                     }
  165.             }
  166.      OutOfMemPoint:
  167.         Error = AEDisposeDesc(&DocList);
  168.         ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleODoc error."));
  169.         APRINT(("-MyHandleODoc"));
  170.         return Error;
  171.     }
  172.  
  173.  
  174. /* handle a quit event */
  175. static pascal    OSErr        MyHandleQuit(AppleEvent* theAppleEvent,
  176.                                                 AppleEvent* reply, long handlerRefcon)
  177.     {
  178.         OSErr            Error;
  179.  
  180.         APRINT(("+MyHandleQuit"));
  181.         /* check for missing required parameters */
  182.         Error = MyGotRequiredParams(theAppleEvent);
  183.         ERROR(Error!=noErr,PRERR(AllowResume,"CApplication::MyHandleQuit error."));
  184.         if (Error != noErr) return Error;
  185.         QuitPending = True;
  186.         APRINT(("-MyHandleQuit"));
  187.         return noErr;
  188.     }
  189.  
  190.  
  191. /* compile a list of files that should be opened when the program starts up. */
  192. /* The parameters should be exactly the ones passed into main() upon startup. */
  193. /* It is implementation defined as to whether they will be used; the Macintosh */
  194. /* does not use them, but uses Apple Events for opening startup documents instead. */
  195. void                    PrepareStartupDocuments(int argc, char* argv[])
  196.     {
  197.         APRINT(("+PrepareStartupDocuments"));
  198.         ERROR(Initialized,PRERR(ForceAbort,
  199.             "PrepareStartupDocuments called multiple times"));
  200.         EXECUTE(Initialized = True;)
  201.         StartupList = NIL;
  202.         AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,
  203.             NewAEEventHandlerProc(&MyHandleOApp),0,False);
  204.         AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,
  205.             NewAEEventHandlerProc(&MyHandleODoc),0,False);
  206.         AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,
  207.             NewAEEventHandlerProc(&MyHandleQuit),0,False);
  208.         APRINT(("-PrepareStartupDocuments"));
  209.     }
  210.  
  211.  
  212. /* Get a startup item.  It will initially return False.  Once the open event is */
  213. /* received, it will return True from then on.  If there is a file specification */
  214. /* to get, it will be returned, otherwise NIL will be returned.  The file */
  215. /* specification should be disposed of with DisposeFileSpec.  Here's how you know if */
  216. /* you should open an untitled document:  the first time it returns True, if it */
  217. /* also returns NIL, then do it. */
  218. MyBoolean            GetStartupObject(struct FileSpec** ReturnStuff)
  219.     {
  220.         ERROR(!Initialized,PRERR(ForceAbort,"GetStartupList:  StartupOpen not initialized"));
  221.         if (StartupList == NIL)
  222.             {
  223.                 return False;
  224.             }
  225.          else
  226.             {
  227.                 if (ArrayGetLength(StartupList) != 0)
  228.                     {
  229.                         *ReturnStuff = (FileSpec*)ArrayGetElement(StartupList,0);
  230.                         ArrayDeleteElement(StartupList,0);
  231.                     }
  232.                  else
  233.                     {
  234.                         *ReturnStuff = NIL;
  235.                     }
  236.                 return True;
  237.             }
  238.     }
  239.  
  240.  
  241. /* clean up any internal structures allocated by PrepareStartupDocument. */
  242. void                    ClearStartupDocuments(void)
  243.     {
  244.         APRINT(("+ClearStartupDocuments"));
  245.         ERROR(!Initialized,PRERR(ForceAbort,"GetStartupList:  StartupOpen not initialized"));
  246.         if (StartupList != NIL)
  247.             {
  248.                 while (ArrayGetLength(StartupList) != 0)
  249.                     {
  250.                         ReleasePtr((char*)ArrayGetElement(StartupList,0));
  251.                         ArrayDeleteElement(StartupList,0);
  252.                     }
  253.                 DisposeArray(StartupList);
  254.                 StartupList = NIL;
  255.             }
  256.         AERemoveEventHandler(kCoreEventClass,kAEOpenApplication,
  257.             NewAEEventHandlerProc(&MyHandleOApp),False);
  258.         AERemoveEventHandler(kCoreEventClass,kAEOpenDocuments,
  259.             NewAEEventHandlerProc(&MyHandleODoc),False);
  260.         AERemoveEventHandler(kCoreEventClass,kAEQuitApplication,
  261.             NewAEEventHandlerProc(&MyHandleQuit),False);
  262.         APRINT(("-ClearStartupDocuments"));
  263.     }
  264.  
  265.  
  266. /* this returns True if the system would like the program to quit.  The program */
  267. /* should then ask the user if he wants to save all changed documents. */
  268. /* A Quit event on the Macintosh will cause this to return True */
  269. MyBoolean            CheckQuitPending(void)
  270.     {
  271.         return QuitPending;
  272.     }
  273.  
  274.  
  275. /* If some other signal besides an implementation defined system quit signal */
  276. /* is received, this can be used to indicate such, and cause a normal shutdown */
  277. /* of the program to occur. */
  278. void                    SetQuitPending(void)
  279.     {
  280.         APRINT(("+SetQuitPending"));
  281.         QuitPending = True;
  282.         APRINT(("-SetQuitPending"));
  283.     }
  284.  
  285.  
  286. /* If the user cancels the quit, this should be used to clear the flag and */
  287. /* allow the program to continue running */
  288. void                    AbortQuitInProgress(void)
  289.     {
  290.         APRINT(("+AbortQuitInProgress"));
  291.         QuitPending = False;
  292.         APRINT(("-AbortQuitInProgress"));
  293.     }
  294.